package analyzator;
/**
 * This class parses the specified string. 
 * After parsing can be returned: 
 * 	input, 
 * 	output,
 * 	set of AnalyzatorCommands.
 * @author Josef Vopalecky
 * 
 */
public class Analyzator {
	String input = null;
	String output = null;
	AnalyzatorCommands[] commands;
	
	/**
	 * The constructor parses the specified string.
	 * @param command parsed string
	 */
	public Analyzator(String command) {
		String[] comms = command.split("\\|");
		if(comms[0].contains("<<")){
			input = comms[0].substring(comms[0].lastIndexOf("<")+1, comms[0].length());
			input = input.trim();
			comms[0] = comms[0].substring(0,comms[0].indexOf("<"));
			if(comms.length == 1){ // Input may be followed by output.
				if(input.contains(">>")){
					output = input.substring(input.lastIndexOf(">")+1, input.length());
					output = output.trim();
					input = input.substring(0,input.indexOf(">"));
				}
			}
		}
		
		if(comms[comms.length-1].contains(">>")){
			output = comms[comms.length-1].substring(comms[comms.length-1].lastIndexOf(">")+1, comms[comms.length-1].length());
			output = output.trim();
			comms[comms.length-1] = comms[comms.length-1].substring(0,comms[comms.length-1].indexOf(">"));
		}
		
		commands = new AnalyzatorCommands[comms.length];
		
		for (int i =0; i < comms.length; i++) {
			comms[i] = comms[i].trim();
			String[] partOfComm = comms[i].split(" ");
			String arguments = "";
			String parameters = "";
			AnalyzatorCommands tmp = new AnalyzatorCommands();
			tmp.setName(partOfComm[0]);
			for(int j = 1; j<partOfComm.length; j++){
				if(partOfComm[j].startsWith("-")){
					if(!parameters.equals("")){
						parameters = parameters.concat(" " + partOfComm[j]);
					}
					else{
						parameters = partOfComm[j];
					}
				}
				else{
					if(!arguments.equals("")){
						arguments = arguments.concat(" " + partOfComm[j]);
					}
					else{
						arguments = partOfComm[j];
					}					
				}
			}
			
			if(arguments != ""){
				tmp.setArguments(arguments.split(" "));
			}
			if(parameters != ""){
			    tmp.setParameters(parameters.split(" "));
			}
			
			commands[i]=tmp;
		}
	}

	/**
	 * 
	 * @return input String
	 */
	public String getInput() {
		return input;
	}

	/**
	 */
	public String getOutput() {
		return output;
	}

	/**
	 * @return Array of Command
	 */
	public AnalyzatorCommands[] getCommands() {
		return commands;
	}
	
	
}
